package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Supplier;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.ISupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("supplier")
@Api(value = "供应商的操作")
public class SupplierController {
    @Autowired
    ISupplierService iSupplierService;
    
    //分页查询
    @GetMapping("/slist")
    @ApiOperation(value = "分页查询供应商目录")
    public ResultResponse<PageInfo<Supplier>> clist(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "5")Integer size,
            @ApiParam(name = "name",value = "接收前台传过来供应商的名称")@RequestParam("cons") String cons){
        ResultResponse<PageInfo<Supplier>> result = null;

        try {
            PageInfo<Supplier> pageInfo =iSupplierService.queryByPage(now, size, cons);
            result = new ResultResponse<>(200,"查询供应商目录成功!",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询供应商目录失败!");
        }
        return result ;
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加供应商")
    public ResultResponse<Void> add(@RequestBody Supplier supplier){
        ResultResponse<Void> result = null;

        try {
            iSupplierService.add(supplier);
            result = new ResultResponse<>(200,"添加供应商成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加供应商失败!");
        }
        return result ;
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据id删除供应商")
    public ResultResponse<Void> del(@PathVariable("id") int id){
        ResultResponse<Void> result = null;

        try {
            iSupplierService.remove(id);
            result = new ResultResponse<>(200,"删除供应商成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除供应商失败!");
        }
        return result ;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "修改供应商")
    public ResultResponse<Void> modify(@RequestBody Supplier supplier){
        ResultResponse<Void> result = null;

        try {
            iSupplierService.modify(supplier);
            result = new ResultResponse<>(200,"修改供应商成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改供应商失败!");
        }
        return result ;
    }

    //查询所有供应商
    @GetMapping("/allList")
    @ApiOperation(value = "查询所有供应商")
    public ResultResponse<List<Supplier>> allList(){

        ResultResponse<List<Supplier>> result = null;

        try {
            List<Supplier> lists = iSupplierService.findAll();
            result = new ResultResponse<>(200,"查询所有供应商成功!",lists);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询所有供应商失败!");
        }
        return result ;
    }

    
}
